<template>
    <div style="height:100%">
        <van-tabs v-model="currentTab" class="catalog" title-inactive-color="#999" background="#fff" title-active-color="#075ebb">
            <van-tab v-for="(v, i) in catalogList" :key="i" :name="v.type">
                <div slot="title"  @click="changeTab(v.type)">{{v.name}}</div>
            </van-tab>
        </van-tabs>
        <!-- 列表 -->
        <div class="scroll-box">
            <van-pull-refresh v-model="isRefresh" @refresh="onPulldownTop" loading-text="loading" pulling-text="" loosing-text="" style="height: 100%;">
                <van-list
                    v-model="isLoading"
                    :finished="finished"
                    :finished-text="(finished&&total>0) ? $t('no_more') : ''"
                    loding-text="loading"
                    @load="getFreshCourseList"
                    style="padding-bottom: 56px;"
                    >
                    <course-list-item :list="freshCourseList" v-if="freshCourseList.length>0"></course-list-item>
                    <!--暂无数据-->
                    <div v-if="!isLoading && total === 0" style="height:100%;">
                        <div class="box-table" style="width:100%;height:100%;">
                            <div class="box-table-cell" style="text-align: center;">
                                <div class="no-data">
                                    <img src="../../../../static/images/no-data.png" alt=""/>
                                    <p class="color999">{{ $t("no_data") }}</p>
                                </div>
                            </div>
                        </div>
                    </div>
                    <common-loading :isLoading="isLoading"></common-loading>
                </van-list>
            </van-pull-refresh>
        </div>
    </div>
</template>

<script>
import { Tab, Tabs } from 'vant';
import CommonLoading from '@/components/common/loading';
import courseListItem from './courseListItem.vue';
export default {
    name: 'myCourses',
    data() {
        return {
            isLoading: false,
            pageNo: 1, // 默认页码
            itmTypeList: '', //课程类型
            total: 0, // 总数
            maxPageNo: 0, // 最大页数 优化之前无限下拉加载发送请求问题
            freshCourseList: [], // 上拉加载新数据
            currentTab: 'All',
            tndId: 0,
            finished: false,
            isRefresh: false
        };
    },
    components: {
        CommonLoading,
        courseListItem,
        [Tab.name]: Tab,
        [Tabs.name]: Tabs
    },
    props: {
        mlId: {
            type: Number,
            default: 0
        }
    },
    methods: {
        /**
         * 切换tab
         * */
        changeTab(type) {
            this.currentTab = type;
            this.itmTypeList = type;
            this.pageNo = 1;
            this.total = 0;
            this.freshCourseList = [];
            this.getFreshCourseList('refresh'); // 修改学习状态时刷新
        },
        /**
         * 课程列表
         * */
        getFreshCourseList(option) {
            this.isLoading = true;
            this.$axios({
                method: 'POST',
                url: `/app/api/trainee/course/center/list`,
                params: {
                    pageNo: this.pageNo,
                    pageSize: 10
                },
                data: {
                    itmTypeList: this.itmTypeList === 'All' || this.itmTypeList === '' ? null : [this.itmTypeList],
                    isExamInd: false,
                    isMobileInd: true,
                    order: 'time',
                    sort: 'RISE',
                    tndId: this.tndId
                }
            }).then(res => {
                if (res && res.status === 200) {
                    if (option && option === 'refresh') {
                        this.freshCourseList = res.data.results || [];
                    } else {
                        this.freshCourseList = this.freshCourseList.concat(res.data.results);
                    }
                }
                this.total = res.data.totalRecord;
                this.isLoading = false;
                this.pageNo++;
                if (this.freshCourseList.length >= this.total) {
                    this.finished = true;
                }
            });
        },
        /*下拉刷新*/
        onPulldownTop() {
            this.isRefresh = true;
            setTimeout(() => {
                this.finished = false;
                this.pageNo = 1;
                this.getFreshCourseList('refresh');
                this.isRefresh = false;
            }, 900);
        }
    },
    computed: {
        /**
         *  课程目录列表
         * */
        catalogList() {
            return [
                {id: 1, type: 'All', name: this.$t('type_all_title')},
                {id: 2, type: 'SELFSTUDY', name: this.$t('cos_type_selfstudy')},
                // {id: 3, type: 'CLASSROOM', name: this.$t('cos_type_classroom')},
                // {id: 4, type: 'INTEGRATED', name: this.$t('cos_type_integrated')},
                // {type: 'KNOWLEDGE', name: this.$t('cos_type_topic')},
                {id: 5, type: 'MAP', name: this.$t('cos_type_map')},
                {id: 6, type: 'AUDIOVIDEO', name: this.$t('new_open_course')}
            ];
        },
        /**
         * 学习状态
         * */
        appStatus() {
            return [
                {id: 1, type: 'All', name: this.$t('type_all_title')}, // 全部
                {id: 2, type: 'I', name: this.$t('cos_app_inprogress')}, // 进行中
                {id: 3, type: 'C', name: this.$t('cos_app_completed')}, // 已完成
                {id: 4, type: 'notIAndC', name: this.$t('cos_other')} // 其他
            ];
        }
    },
    mounted() {
        if (this.$route.query.type) {
            this.currentTab = this.$route.query.type;
            this.itmTypeList = this.$route.query.type;
        }
        this.getFreshCourseList('refresh');
    },
    watch: {
        mlId(nV) {
            this.tndId = nV;
            this.pageNo = 1;
            this.getFreshCourseList('refresh');
        }
    }
};
</script>

<style scoped lang="less">
// 公共滑动盒子
.content-box{
    display: -webkit-box;
    overflow-x: scroll;
    -webkit-overflow-scrolling:touch;
    &::-webkit-scrollbar { /**chrome、safari */
        display: none;
    }
    scrollbar-width: none; /**火狐 */
    -ms-overflow-style: none; /**IE10+ */
}
.catalog{
    /deep/.van-tabs__line{
        display: none;
    }
    /deep/.van-tabs--line .van-tabs__wrap{
        height: 50px;
    }
}
.scroll-box{
    height: calc(100% - 50px);
    /deep/.vue-scroll>div {
        height: 100%;
    }
}
/deep/.van-tab{
  flex: 1 0 auto;
}
</style>
